#include "head.h"

int fb_init_mouse(int *fd)
{
	*fd = open(MOUSE_DEVICE, O_RDONLY | O_NONBLOCK);
	if(fd < 0)
	{
		fprintf(stderr, "Open %s failed: %s\n", MOUSE_DEVICE, strerror(errno));
		return -1;

	}
	return 0;
}

#if 1
int mouse_parse(const u8_t *buf, mouse_event_t* mevent)
{
	switch(buf[0] & 0x7){
	case 1:						/* left */
		mevent->button = 1;
		break;
	case 2:
		mevent->button = 2;			/* right */
		break;
	case 4:
		mevent->button = 3;			/* middle */
		break;
	default:
		break;
	}

	mevent->x =  buf[1] - ((buf[0] & 0x10) ? 256 : 0);
	mevent->y = -buf[2] + ((buf[0] & 0x20) ? 256 : 0); 

	return 0;
}
#endif

#if 1
int fb_restorecursor(fb_info my_fb, int x, int y)
{
	int i, j;
	u32_t color;
	for (j = 0;j<C_HEIGHT;j++){
		for (i = 0;i<C_WIDTH;i++){
			color = save_cursor[i + j * C_WIDTH];
			fb_point(my_fb,x+i,y+j,color);
		}
	}
	
	return 0;
}
#endif

#if 1
int fb_savecursor(fb_info my_fb,int x,int y)
{
	int i,j;
	u32_t color;

	for (j = 0; j < C_HEIGHT; j++)
	{
		for (i = 0; i < C_WIDTH; i++)
		{
  			color = *(u32_t*)(my_fb.fbmem + ((y + j) * my_fb.w + x + i) * my_fb.bpp/8);
			save_cursor[i + j * C_WIDTH] = color;
		}
	}
	return 0;
}

#endif

#if 1
int fb_drawcursor(fb_info my_fb, int x, int y)
{
	int i,j;
	unsigned int color;

	fb_savecursor (my_fb,x,y);

	for (j = 0;j<C_HEIGHT;j++)
	{
		for (i = 0;i<C_WIDTH;i++)
		{
			color = cursor_pixel[i + j * C_WIDTH];
			if (color != T___)
				fb_point(my_fb,x+i,y+j,color);
		}
	}
	return 0;
}
#endif 
